<?php
ini_set("display_errors", "on");
error_reporting(E_ALL | E_STRICT);
require_once("classes/XForms.php");
if(isset($_GET["si"])){
  ob_start();
  session_start();
  ob_end_clean();
  $xp = $_SESSION["xforms"][$_GET["si"]];
  /*Actualiza los datos*/
  if(!empty($_GET["da"])) {
    $newDoc = new DOMDocument();
    $newDoc->loadXML($_GET["da"]);
    $newData=$newDoc->childNodes->item(0);
    $model = $xp->getDefaultModel();
    $instance = $xp->getDefaultInstance($model);
    $data = $xp->getFirstElement($instance);
    $instance->removeChild($data);
    $instance->appendChild($newData);
  }
  /**/
  $binding = $xp->findBinding($_GET["xp"], $xp->document);
  $xBinding = $xp->findXElement($binding);
  if($xBinding) {
    $res = call_user_func_array(array($xBinding, $_GET["fn"]),json_decode($_GET["pa"]));
    echo json_encode($res);
  }
  exit;
} else {
  session_start();
}
//header("Content-type: application/xhtml+xml");
ob_start();
//CARGA EL FORMULARIO XFORM
$xp = new XForms();
//$_SESSION["xforms"]=array();
//$_SESSION["xforms"]["0"]=$xp;
$x = $xp->load(isset($_GET['f']) ? $_GET['f'] : 'examples/test1.xform');
$html = $xp->render();
$output = ob_get_contents();
ob_end_clean();
//Imprime el log dentro del $html
$html=str_replace("/**@debug**/", $output, $html);
//IMPRIME EL RESULTADO
echo $html;
